<?php
/**
 * Plugin Name:Ping服务
 * Version:2.2
 * Description:当你更新博客时自动通知搜索引擎
 * Author:vibbow
 * Author Email:vibbow@gmail.com
 * Author URL:http://vsean.net/
 */

!defined('EMLOG_ROOT') && exit('access deined!');

//后台侧边栏挂载
function ping_menu() {
	echo '<div class="sidebarsubmenu" id="ping"><a href="./plugin.php?plugin=ping">Ping服务</a></div>';
}
addAction('adm_sidebar_ext', 'ping_menu');

//Ping主函数
function ping($blogid) {
	if (!extension_loaded('curl')) {
		return 0;
	}

	//获取信息
	$ping_blogname = Option::get('blogname');
	$ping_isurlrewrite = Option::get('isurlrewrite');
	$ping_isalias = Option::get('isalias');
	$ping_isalias_html = Option::get('isalias_html');

	$ping_list = ping_getconfig('ping_list');
	$ping_url = @unserialize($ping_list);

	if (empty($ping_url)) {
		return 0;
	}

	global $action;

	if ($action == 'autosave') {
		return 0;
	}

	$ping_loginfo = ping_getlog($blogid);

	if ($ping_loginfo['hide'] == 'y') {
		return 0;
	}

	//判断日志是否已经ping过
	$ping_history = ping_getconfig('ping_history');
	if (strpos($ping_history, "|{$blogid}|") !== FALSE) {
		return 0;
	}

	$ping_title = addslashes($ping_blogname);
	$ping_add = BLOG_URL;
	$ping_rss = BLOG_URL.'rss.php';
	$ping_chk = Url::log($blogid);
	
	//定义要Ping给搜索引擎的XML信息
	$ping_header = array('Content-Type: text/xml; charset=UTF-8', 'User-Agent: Emlog Ping Plugin/2.2');

	$ping_body  = '<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>weblogUpdates.extendedPing</methodName><params>';
	$ping_body .= '<param><value>'.$ping_title.'</value></param>';
	$ping_body .= '<param><value>'.$ping_add.'</value></param>';
	$ping_body .= '<param><value>'.$ping_chk.'</value></param>';
	$ping_body .= '<param><value>'.$ping_rss.'</value></param>';
	$ping_body .= '</params></methodCall>';

	//创建一个cUrl多线程句柄
	$ping_mh = curl_multi_init();

	//将每个线程的设置加入到多线程cUrl
	for ($ping_count = 0; $ping_count < sizeof($ping_url); $ping_count++) {
		$ping_ch[$ping_count] = curl_init();
		curl_setopt($ping_ch[$ping_count], CURLOPT_URL, $ping_url[$ping_count]);
		curl_setopt($ping_ch[$ping_count], CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ping_ch[$ping_count], CURLOPT_TIMEOUT, 10);
		curl_setopt($ping_ch[$ping_count], CURLOPT_HTTPHEADER, $ping_header);
		curl_setopt($ping_ch[$ping_count], CURLOPT_POST, 1 );
		curl_setopt($ping_ch[$ping_count], CURLOPT_POSTFIELDS, $ping_body);
		curl_multi_add_handle($ping_mh, $ping_ch[$ping_count]);
	}

	//执行多线程访问
	do {
		curl_multi_exec($ping_mh,$ping_flag);
	} while ($ping_flag > 0);

	//保存到已ping列表
	$ping_history .= "|{$blogid}|";
	ping_putconfig('ping_history', $ping_history);

	//保存返回的信息
	$ping_log = date("Y-m-d H:i:s")."\r\n";
	for ($ping_count = 0; $ping_count < sizeof($ping_url); $ping_count++) {
		$ping_results = curl_multi_getcontent($ping_ch[$ping_count]);
		$ping_parser = xml_parser_create();
		xml_parse_into_struct($ping_parser, $ping_results, $ping_vals);
		xml_parser_free($ping_parser);
		$ping_log .= $ping_url[$ping_count]."\r\n";
		foreach ($ping_vals as $ping_value) {
			if ($ping_value['type'] == 'complete') {
				$ping_log .= $ping_value['tag'].'='.$ping_value['value']."\r\n";
			}
		}
		$ping_log .= "\r\n";
	}
	file_put_contents(dirname(__FILE__).'/ping.log', $ping_log, FILE_APPEND);

	//释放资源
	for ($ping_count=0; $ping_count<sizeof($ping_url); $ping_count++) {curl_multi_remove_handle($ping_mh, $ping_ch[$ping_count]);}
	curl_multi_close($ping_mh);
}
addAction('save_log', 'ping');

//获取日志信息
function ping_getlog($blogid) {
	$ping_db = MySql::getInstance();
	$ping_sql = "SELECT * FROM ".DB_PREFIX."blog where gid = ".$blogid;
	$ping_res = $ping_db->query($ping_sql);
	$ping_loginfo = $ping_db->fetch_array($ping_res);
	return $ping_loginfo;
}

//读取设置内容
function ping_getconfig($name) {
	$kv = new SaeKV();
    // 初始化KVClient对象
    $ret = $kv->init();
    $ret = $kv->get($name);
	return $ret;
}

//保存设置内容
function ping_putconfig($name, $value = '') {
	$kv = new SaeKV();
    // 初始化KVClient对象
    $kv->init();
    $kv->set($name, $value);

	if ($kv->errno() != 0) {
		emMsg ($kv->errmsg ());
	}
}
?>
